﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace RetailOnlineSite.Dll
{


    /// 
    /// Creates minimum roles and user for application access.
    /// 
    public class InitSecurity
    {
        // application roles
        public static string[] applicationRoles = { "Administrators", "Users", "Power Users" };

        // super user
        private static string superUser = "admin";
        // default password, should be changed on first connection
        private static string superUserPassword = "admin!@";
        private static string superUserEmailAddress = "huutai.alz@gmail.com!";

        private InitSecurity()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// 
        /// Creates minimal membership environment.
        /// 
        public static void SetupSecurity()
        {
            SetupRoles();
            SetupSuperuser();
        }

        /// 
        /// Checks roles, creates missing.
        /// 
        public static void SetupRoles()
        {
            // create roles
            foreach (var item in applicationRoles)
            {
                if (!Roles.RoleExists(item))
                    Roles.CreateRole(item);
            }
        }
        /// Checks if superuser account is created.
        /// Creates the account and assigns it to all roles.
        /// 
        public static void SetupSuperuser()
        {
            // create super user
            MembershipUser user = Membership.GetUser(superUser);
            if (user == null)
                Membership.CreateUser(superUser, superUserPassword, superUserEmailAddress);

            // assign superuser to roles
            Roles.AddUserToRoles(superUser, applicationRoles);
            
        }
    }
}
